/**
 * @author MIAO
 * Created on 2011-10-7
 * For more information, please visit:ismiao.com
 */
package com.ismiao.spring.jdbcTemplate;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class TestSpringJDBCTemplate {
	DriverManagerDataSource dataSource = new DriverManagerDataSource();
	JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

	// 默认的构造函数初始化对象
	public TestSpringJDBCTemplate() {
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/test");
		dataSource.setUsername("root");
		dataSource.setPassword("root");
	}

	// 更新操作
	public void update() {
		jdbcTemplate
				.update("UPDATE users SET userDesc = 'fuzhou' WHERE userID = 10001");
	}

	// 查询操作
	public void select() {
		String userDesc = jdbcTemplate.queryForObject(
				"select userDesc from users where userID=10001",
				java.lang.String.class);
		System.out.println("userDesc:" + userDesc);
	}

	public static void main(String[] args) {
		// 更新数据库
		new TestSpringJDBCTemplate().update();
		// 查询数据库
		new TestSpringJDBCTemplate().select();
	}
}
